﻿using System;
using System.Collections.Generic;

namespace LaoLuo
{
    /// <summary>
    /// 提供枚举的扩展方法
    /// </summary>
    public static class EnumExtensions
    {
        #region flag extensions

        /// <summary>
        /// gets a value indicates whether <paramref name="flagToTest"/> is set in this flag-value
        /// </summary>
        /// <typeparam name="T">type of flags</typeparam>
        /// <param name="flags">value to check whether another flag is set in it</param>
        /// <param name="flagToTest">flag to test</param>
        /// <returns>if <paramref name="flagToTest"/> is set in this flag, return true, else false</returns>
        public static Boolean IsSet<T>(this T flags, T flagToTest)
             where T : struct, IComparable, IFormattable, IConvertible
        {
            var t = Convert.ToUInt32(flagToTest);
            if (t == 0)
                throw new ArgumentOutOfRangeException("flagToTest", "Value must not be 0");
            return (Convert.ToUInt32(flags) & t) == t;
        }

        #endregion

        /// <summary>
        /// 获取枚举的所有值
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="value">值</param>
        /// <returns>枚举的所有值</returns>
        public static IEnumerable<T> GetAllEnumValues<T>(this T value)
             where T : struct, IComparable, IFormattable, IConvertible
        {
            return EnumHelper.GetAllEnumValues<T>();
        }

        /// <summary>
        /// gets a value indicates whether this value is defined in enum
        /// </summary>
        /// <typeparam name="T">type of enum</typeparam>
        /// <param name="value">value to check</param>
        /// <returns>if this value is defined in enum, return true , else false</returns>
        public static bool IsDefinedInEnum<T>(this T value)
             where T : struct, IComparable, IFormattable, IConvertible
        {
            return EnumHelper.IsDefinedInEnum<T>(value);
        }
    }
}
